package cn.gson.springboot.model.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.gson.springboot.model.dao.AddrecordDAO;
import cn.gson.springboot.model.dao.CkDAO;
import cn.gson.springboot.model.dao.DrugsYkDao;
import cn.gson.springboot.model.dao.FankuDetaiDAO;
import cn.gson.springboot.model.dao.InventoryDAO;
import cn.gson.springboot.model.dao.InventorydetaiDAO;
import cn.gson.springboot.model.dao.RedetaiDAO;
import cn.gson.springboot.model.dao.SpenddetaiDAO;
import cn.gson.springboot.model.mapper.PDMapper;
import cn.gson.springboot.model.pojos.Apply;
import cn.gson.springboot.model.pojos.Applydetai;
import cn.gson.springboot.model.pojos.Ck;
import cn.gson.springboot.model.pojos.DrugsYk;
import cn.gson.springboot.model.pojos.Inventory;
import cn.gson.springboot.model.pojos.Inventorydetai;
import cn.gson.springboot.model.vo.InventoryVO;

@Service
@Transactional(rollbackFor=Exception.class)
public class PDService {
	private int a;
	private int a4;
	@Autowired
	private PDMapper tm;
	@Autowired
	private InventoryDAO od;
	@Autowired
	private DrugsYkDao ddao;
	@Autowired
	private InventorydetaiDAO dd;
	@Autowired
	private CkDAO ck;
	@Autowired
	private FankuDetaiDAO fd;
	@Autowired
	private SpenddetaiDAO sd;
	@Autowired
	private RedetaiDAO rd;
	@Autowired
	private AddrecordDAO ad;
	//查询所有药品
	public List<Map<String,Object>> cxsy() {
		List<Map<String,Object>> ll= tm.cxsy(); 
		return ll;
	}
	public List<Map<String,Object>> pdxq(int a) {
		List<Map<String,Object>> ll= tm.pdxq(a); 
		return ll;
	}
	public List<Map<String,Object>> pdmccx(String a) {
		List<Map<String,Object>> ll= tm.pdmccx(a); 
		return ll;
	}
	//申请表主从表添加
	public void saveOrder(InventoryVO svo) {

		//获取采购订单进行新增
		Inventory order = svo.getOrder();
		
		List<Inventorydetai> details = svo.getDetails();
		//循环
		for (Inventorydetai pur : details) {
			//记录表编号
			pur.setInventory(order);
			//药品表编号
			DrugsYk drug = ddao.findById(pur.getDrugsYk().getDrugId()).get();
			pur.setDrugsYk(drug);
			Ck c=ck.kmpccx(pur.getDrugsYk().getDrugId(), pur.getYppc());
			//库存数量  
			int sl=c.getDrugs_Stock_kc();  
			
			pur.setInventorydetai_number(sl);
			pur.setYppc( pur.getYppc());
			
		//实际数量
		int sjsl=pur.getInventorydetai_sl();
		System.out.println("实际数量是："+sjsl);
		pur.setInventorydetai_sl(sjsl);
			DrugsYk d=ddao.cxdr(pur.getDrugsYk().getDrugId());
			//卖出价格
			int mc=  d.getDrugSellingPrice().intValue();
			//买入价格
			int mr=d.getDrugBuyingPrice().intValue();
			//利润
			int lr=mc-mr;
			System.out.println("利润是"+lr);	
			try {
				 //新增数量
				JSONArray sl4=tm.cxxzsy(pur.getDrugsYk().getDrugId(), pur.getYppc());
				a4=sl4.getJSONObject(0).getInteger("sl");
				System.out.println("新增数量是"+a4);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			if(sl<=sjsl) {
				//实际利润
				int sy=(a4-sjsl)*lr;
				pur.setKuiyings("盈"); 
				pur.setSyjieyu(sy);
				 pur.setKuiLy("正常");
			}else {
				//应有利润
				int sy=(a4-sjsl)*lr;
				pur.setKuiyings("亏");
				a=1;
				pur.setSyjieyu(sy);
				pur.setKuiLy("数量不对");
			}
			//判断总亏盈 
			if(a==0) {
				System.out.println("我又到了循环了");
				order.setKuiying("盈");
			}else if(a==1) {
				System.out.println("我又到了循环了1");
				order.setKuiying("亏");
			}
			System.out.println("我到了最后一步了");
			
			Inventory save = od.save(order);
			
			System.out.println("我真的已经进过判断了");
			dd.save(pur);
			
		}
	
		//pddao.saveAll(details);
	}
}
